import random
from flask import Blueprint, request
from static.connect.conn_mysql import Connect


app_phone_verify = Blueprint('app_phone_verify', __name__)


def send_sms(phone):  # 发送短信

    phone_code = random.randint(100000, 999999)

    with Connect() as c:

        sql = "INSERT INTO vcode(phone, verify_code) VALUES (%s, %s)"

        c.insert(sql, (phone, phone_code))

    print(f'给电话：{phone},已经发送短信，短信验证码为{phone_code}')


@app_phone_verify.route('/app_phone_verify', methods=['POST'])
def phone_notify():
    phone = request.form.get('phone')
    send_sms(phone)

    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": "短信已经发送到你的手机"
    }
